package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.query.ClassinfoQueryObject;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("classinfo")
public class ClassinfoController {
    @Autowired
    private IClassinfoService classinfoService;

    @Autowired
    private IEmployeeService employeeService;
    //处理查询所有
    @RequestMapping("list")
    public String list (Model model, @ModelAttribute("qo") ClassinfoQueryObject qo) {
        PageInfo<Classinfo> pageInfo = classinfoService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees",employees);

        return "classinfo/list";//找视图
    }


    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete (Long id) {
        if (id != null) {
            classinfoService.deleteById(id);
        }
        return new JsonResult();//让浏览器重新发一次请求
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate (Classinfo classinfo) {
        if (classinfo.getId() == null) {
            classinfoService.save(classinfo);
        } else {
            classinfoService.updateById(classinfo);
        }
        return new JsonResult();//让浏览器重新发一次请求
    }
}
